import { observable, action } from 'mobx';
import BaseListStore from './BaseListStore';
import * as Apis from '../apis/CustomerReportApi';
import { WeaForm } from 'comsMobx';
import { message } from 'antd';
import { WeaTools } from 'ecCom';
import { toJS } from 'mobx';
import { getEveList } from '../apis/ReportApi';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

class AddressChartStore {
    condition = [];
    @observable customerTypeList = [];
    @observable showSearchAd = false;
    @observable loading = false;
    @observable form = new WeaForm();
    @observable options = [];
    @observable mapDatas = [];
    @observable pieDatas = [];
    @observable totalValue = 0;
    @observable currentProName = getLabel(84378, '全国');
    @observable selectProvince = {
      provinceId: '0',
    }
    @observable dialogParams ={
      visible: false,
      url: '/spa/crm/static/index.html#/main/crm/customer/report?',
    }

    @action
    initData() {
      Apis.getAddressChartCondition().then((data) => {
        this.condition = data.datas;
        this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
      });

      Apis.getCustomerTypes().then((data) => {
        this.customerTypeList = data.customerTypeList;
      });
    }

    doSearch=(params = {}) => {
      const _this = this;
      this.loading = true;
      const searchParams = this.form.getFormParams();
      const newParams = { ...this.selectProvince, ...searchParams, ...params };
      Apis.getMapProvince(newParams).then((data) => {
        if (data.status == 'success') {
          this.currentProName = getLabel(84378, '全国');
          this.mapDatas = data.datas.provinces;
          this.pieDatas = data.datas.provinces;
          this.options = data.datas.options;
          this.totalValue = data.datas.totalValue;
          _this.loading = false;
          this.selectProvince = { provinceId: WeaTools.getSelectDefaultValue(data.datas.options) };
        } else {
          message.error(data.msgCode);
        }
      });
    }

    searchCityCus= (id) => {
      this.loading = true;
      this.selectProvince = {
        provinceId: id,
      };
      const newMapDatas = toJS(this.mapDatas);
      for (const provinceObj of newMapDatas) {
        if (provinceObj.id == id) {
          provinceObj.selected = true;
        } else {
          provinceObj.selected = false;
        }
      }
      this.mapDatas = newMapDatas;
      if (id == '0') { // 全国
        this.doSearch();
      } else {
        const params = { ...this.form.getFormParams() };
        for (const item in params) {
          if (params[item] == '') {
            delete params[item];
            continue;
          }
        }
        Apis.getMapCity({ provinceId: id, ...params }).then((data) => {
          this.pieDatas = data.datas.cityes;
          this.totalValue = data.datas.totalValue;
          this.currentProName = data.datas.provinceName;
          this.loading = false;
        });
      }
    }

    handleCustomersDialog=(bool, id) => {
      const params = { ...this.form.getFormParams() };
      let paramsStr = '';
      for (const item in params) {
        if (params[item] == '') {
          delete params[item];
          continue;
        }
        paramsStr += `&${item}=${params[item]}`;
      }
      // console.log(paramsStr)
      if (bool) {
        let url = '';
        if (this.selectProvince.provinceId !== '0') {
          url = `/spa/crm/static/index.html#/main/crm/customer/report?city=${id}${paramsStr}`;
        } else {
          url = `/spa/crm/static/index.html#/main/crm/customer/report?province=${id}${paramsStr}`;
        }
        this.dialogParams = { url, visible: bool };
      } else {
        this.dialogParams = { ...this.dialogParams, visible: bool };
      }
    }

    setShowSearchAd = (bool) => {
      this.showSearchAd = bool;
    }

    clearAdFields = () => {
      this.form.reset();
    }
}

export default AddressChartStore;
